import {Component, OnInit, OnDestroy, Input} from '@angular/core';
import {Operation} from "./operation";
import {ActivatedRoute} from "@angular/router";
import {API} from "../../constants/constants";
import {ResponseUtil} from "../../util/response-util";
import {RequestUtil} from "../../util/request-util";

@Component(
  {
    selector:"[data-role=one-operation]",
    templateUrl: "./one-operation.html"
  }
)
export class OneOperationComponent implements OnInit, OnDestroy{
  private _include;
  private _exclude;
  private _proxyMethods;
  @Input() set include(include: Array<string>) {
    this._include = include;
  }
  @Input() set exclude(exclude: Array<string>) {
    this._exclude = exclude;
  }
  @Input() set proxyMethods(proxyMethods: any) {
    this._proxyMethods = proxyMethods;
  }
  get include():  Array<string>{ return this._include }
  get exclude():  Array<string> { return this._exclude }
  get proxyMethods():  any { return this._proxyMethods }
  // 操作按钮集合
  operations: Operation[];
  // 方法代理
  proxy: object;

  constructor(
    private route: ActivatedRoute,
    private requestUtil: RequestUtil,
    private responseUtil: ResponseUtil
  ) {}

  ngOnInit() {
   this.getOperations();
  }

  ngOnDestroy() {
    this.proxy = null;
  }

  /**
   * 获取当前菜单下操作操作
   */
  private getOperations(){
    let id = this.route.snapshot.params['mid'];
    if(!id){
      return;
    }
    let uri = API.getMenuOwnedOperations.replace("{id}",id);
    this.requestUtil.get(uri)
      .subscribe(
        result => this.responseUtil.success(result,(result) => {
          if (!this.exclude && !this.include) {
            this.operations = result.data;
            return false;
          } else if (this.exclude && this.include){
            const excludeArr = this.excludeData(result.data);
            this.operations = this.includeData(excludeArr);
          } else {
            if (this.exclude) {
              this.operations = this.excludeData(result.data);
            } else if (this.include) {
              this.operations = this.includeData(result.data);
            }
          }
        }),
        error => this.responseUtil.error(error)
      );
  }
  /**
   * 过滤排斥数据
   * @param excludeArr
   */
  excludeData = (excludeArr) => {
    if (this.exclude.length > 0) {
      return excludeArr.filter((val, i) => {
        let isInclude = false;
        this.exclude.every((code , i) => {
          if (val.operationCode.indexOf(code) === -1) {
            isInclude = true;
            return false;
          } else {
            return true;
          }
        });
        return isInclude;
      });
    } else {
      return excludeArr;
    }
  };
  /**
   * 过滤包含数据
   * @param includeData
   */
  includeData = (includeData) => {
    if (this.include.length > 0) {
      return includeData.filter((val, i) => {
        let isInclude = false;
        this.include.every((code , i) => {
          if (val.operationCode.indexOf(code) !== -1) {
            isInclude = true;
            return false;
          } else {
            return true;
          }
        });
        return isInclude;
      });
    } else {
      return includeData;
    }
  };
  /**
   * 调用代理
   * @param callback
   */
  execute(callback: string) {
    if (!this.proxy && this.proxyMethods) {
      this.proxy = this.proxyMethods;
    }
    if (callback && this.proxy && this.proxy.hasOwnProperty(callback)) {
      typeof this.proxy[callback] === 'function' && this.proxy[callback]();
    }
  }
}
